/**
 * @Description: 自动化推送
 * @author Beon
 * @date 2021/1/14
*/
import {OraLoading} from '../decorators/oraLoading';
import {Command} from '../types/command';
import {ClassTool} from '../decorators/classTool';
import {NetCode} from '../git/netCode';
import {BranchOrigin} from '../git/branchOrigin';

export class LzPush implements Command{

    @OraLoading.beProxy
    status: OraLoading // 执行状态

    constructor() {
        this.status.value = '开始推送前处理';
    }

    /**
     * 指令运行函数
     * @param {[String]} arg 参数
     * @returns {Promise<void>}
     */
    @ClassTool.saveThis
    async done(arg?: [string]): Promise<void> {
        await LzPush.infoDeal(arg ? arg[0] : undefined);

        let index = 0;
        while (index < BranchOrigin.gitOrigin.length) {
            this.status.value = `推送${BranchOrigin.gitOrigin[index]}远程库`;
            await LzPush.pushDeal(BranchOrigin.gitOrigin[index], arg ? arg[0] : undefined);
            index++;
        }

        this.status.value = '';
    }

    /**
     * pull处理
     * @param repoPath
     * @returns {Promise<void>}
     */
    static async infoDeal(repoPath?: string): Promise<void> {
        await BranchOrigin.dealBranchMessage(repoPath);
        await BranchOrigin.dealOriginMessage(repoPath);
    }

    /**
     * push处理
     * @param repoPath
     * @param {String} origin 执行origin位置
     * @returns {Promise<void>}
     */
    static async pushDeal(origin:string, repoPath?: string): Promise<void> {
        await NetCode.rebasePull(origin, repoPath);
        await NetCode.codePush(origin, repoPath);
    }
}
